﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using Web.HelperClasses;
using NHibernate.Criterion;
using Web.HelperClasses.AvatarService;

namespace Web.Model
{
        public class News : Entity, IPhotoContainer, IOwnable, ICommentable
        {
            public News()
            {
                HeadComments = new List<HeadComment>();
                Photos = new List<Photo>();
            }

            public User Author { get; set; }

            public string Title { get; set; }

            public string ShortContent { get; set; }

            public string Content { get; set; }

            public DateTime DateCreated { get; set; }

            public bool IsHidden { get; set; }

            public IList<HeadComment> HeadComments { get; set; }

            public Avatar NewsAvatar { get; set; }

            public IList<Photo> Photos { get; set; }

            public string AlbumName
            {
                get
                {
                    int limit = 50;
                    string ShortTitle = Title.Length > limit ? Title.Substring(0, limit) + "..." : Title;
                    return String.Format("{0} (#{1})", ShortTitle, Id);
                }
            }

            public void Clear(ISession session)
            {
                YandexFotki yandexFotki = YandexFotki.GetInstance();
                Photos.ToList().ForEach(photo => { yandexFotki.Delete(photo); session.Delete(photo); });
                AvatarService avatarService = new NewsAvatarService(session, yandexFotki);
                if (NewsAvatar != null)
                {
                    avatarService.RemoveAvatar(NewsAvatar);
                    NewsAvatar = null;
                }
                var HeadComments = session.CreateCriteria<HeadComment>()
                    .Add(Restrictions.Eq("Story", this))
                    .List<HeadComment>();
                HeadComments.ToList().ForEach(c => { c.Clear(session); session.Delete(c); });

                session.Save(this);
            }

            public bool CanUpload(User user)
            {
                return user != null && (user.IsAdmin || user.Id == Author.Id); 
            }
            public bool CanDelete(User user)
            {
                return user != null && (user.IsAdmin || user.Id == Author.Id); 
            }

            public bool IsVisibleToUser(User user)
            {
                return !IsHidden || (user != null && (user.IsAdmin || Author.Id == user.Id));
            }

            public User getOwner()
            {
                return this.Author;
            }

            public void NotifyNewComment(ISession session, CommentBase newComment)
            {
                NewsNotifier notifier = new NewsNotifier(Id, session);
                notifier.NotifyNewComment(newComment);
            }
        
        }
    
}